package com.lxl.rabbitmq;

import com.lxl.config.SystemConfig;
import com.lxl.util.PropertyUtil;
import com.rabbitmq.client.ConnectionFactory;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @program: rabbitmq
 * @author: wuchun
 * @description: 消息队列配置
 * @create: 2018/9/29 09:52
 */
@Configuration
public class RabbitConfig {

    private String host = PropertyUtil.getProperty("spring.rabbitmq.host");
    private int port = Integer.parseInt(PropertyUtil.getProperty("spring.rabbitmq.port"));
    private String username = PropertyUtil.getProperty("spring.rabbitmq.username");
    private String password = PropertyUtil.getProperty("spring.rabbitmq.password");

    @Bean
    public ConnectionFactory connectionFactory() {
        ConnectionFactory connectionFactory = new CachingConnectionFactory();
        connectionFactory.setHost(host);
        connectionFactory.setPort(port);
        connectionFactory.setUsername(username);
        connectionFactory.setPassword(password);
        connectionFactory.setVirtualHost("/");
        //消息确认
        connectionFactory.setPublisherConfirms(true);
        connectionFactory.setPublisherReturns(true);
        return connectionFactory;
    }

    @Bean
    public Queue procQueue() {
        return new Queue(SystemConfig.QUEUE_PROC_NAME, true, false, false, null);
    }

    @Bean
    public Queue simpleQueue() {
        return new Queue(SystemConfig.QUEUE_SIMPLE_NAME, true, false, false, null);
    }

}
